package user_account_flow_reports

import (
	"gitee.com/theegg/go-brick/app/model"
	"gitee.com/theegg/go-brick/app/service/app"
)

// 查询参数
type AccountFlowReportQueryParams struct {
	PageSize  string `form:"page_size" json:"page_size"  binding:"gte=0,lte=100" label:"每页数量"`
	PageIndex string `form:"page_index" json:"page_index"  binding:"gte=0,lte=99999" label:"当前页数"`
	UserName  string `form:"username" json:"username"  binding:"" label:"用户名"`
}

// 分页获取所有数据
// @param params map 参数
// @return interface{}, error
func GetDataList(params map[string]interface{}) (map[string]interface{}, error) {
	returnData := make(map[string]interface{}, 0)

	queryParams, err := model.BuildAdminQueryParam(params)
	if err != nil {
		return returnData, nil
	}

	query := app.Db().Desc("id").Limit(queryParams.Limit, queryParams.Offset)

	// 查询用户名 == username
	if queryParams.Params["username"] != "" {
		query.Where("username = ?", queryParams.Params["username"])
	}

	nodeData := make([]UserAccountFlowReports, 0)

	// 求总条数
	total, err := query.FindAndCount(&nodeData)
	if err != nil {
		return nil, app.NewError(err)
	}

	returnData["data"] = nodeData
	returnData["total"] = total

	return returnData, nil
}

// 获取单个数据
// @title GetItemById
// @return *UserAccountFlowReports, error
func GetItemById(id int64) (*UserAccountFlowReports, error) {
	item := new(UserAccountFlowReports)
	has, err := app.Db().ID(id).Get(item)

	if err != nil {
		return nil, app.NewError(err)
	} else {
		if !has {
			return nil, app.NewError("对不起, 不存在的数据")
		}
	}

	return item, nil
}
